<?php
/*
This sends a little bit of JSON to say if the user is logged in. 
*/

require_once( 'core.php' );
require_once( 'current_user_api.php' );

auth_ensure_user_authenticated( );
//helper_begin_long_process( );

$t_plugin_path = config_get( 'plugin_path' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'HTTPclient.php' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'JSONstore.php' );

$refineURI = plugin_config_get( 'refineLocalURI' );
if(!$refineURI){
	$refineURI = plugin_config_get( 'refineBaseURI' );
}


$projectID = $_REQUEST["grProject"]; 
$colIndex = $_REQUEST["colIndex"]+0;
$colName = $_REQUEST["colName"];
$errors = array(); //error response




/*
First get the numeric data.
*/

$engine = array(

	"facets" => array(
		
			array(		  
				"type" => "range",
				"name" => $colName,
				"expression" => "value",
				"columnName" => $colName,
				"selectNumeric" => true,
				"selectNonNumeric" => true,
				"selectBlank" => true,
				"selectError" => true
			)
		),
		"mode" => "row-based"
);

$messageNumeric = array("project" => $projectID, "engine" => json_encode($engine, 1));

$HTTPclient = new HTTPclient;
$HTTPclient->refineBaseURI = $refineURI;
$HTTPclient->postActionURI = "/command/core/compute-facets?project=".$projectID;
$HTTPclient->authorized = true; //no need for more elaboration
$HTTPclient->message = $messageNumeric; //json data as a string
$HTTPclient->postData(); //send the post!
if($HTTPclient->success != false){
	$responseNumeric = json_decode($HTTPclient->responseData, 1);
}
else{
	$responseNumeric = false;
	$errors["colNumeric"] = $HTTPclient->errorMessage;
}
unset($HTTPclient);


/*
Now get the textual data

*/
$engine["facets"][0]["type"] = "list";
unset($engine["facets"][0]["selectNumeric"]);
$engine["facets"][0]["selection"] = array();

$messageText = array("project" => $projectID, "engine" => json_encode($engine, 1));

$HTTPclient = new HTTPclient;
$HTTPclient->refineBaseURI = $refineURI;
$HTTPclient->postActionURI = "/command/core/compute-facets?project=".$projectID;
$HTTPclient->authorized = true; //no need for more elaboration
$HTTPclient->message = $messageText; //json data as a string
$HTTPclient->postData(); //send the post!
if($HTTPclient->success != false){
	$responseText = json_decode($HTTPclient->responseData, 1);
}
else{
	$responseText = false;
	$errors["colText"] = $HTTPclient->errorMessage;
}
unset($HTTPclient);



$resMessage = array("colIndex" => $colIndex,
		    "colNumeric" => $responseNumeric,
		    "colText" => $responseText,
		    "errors" => $errors);
$output = json_encode($resMessage, 1);
header('Content-Type: application/json; charset=utf8');
echo $output;

?>
